package com.bomb.disruptor.base;

import com.lmax.disruptor.EventHandler;

import java.util.Date;

/**
 * 事件消费者，也就是一个事件处理器。这个事件处理器简单地把事件中存储的数据打印到终端
 *
 * @author wxh
 */
public class LongEventHandler implements EventHandler<LongEvent> {
    private int ordinal; // 当前消费线程的编号，需要指定
    private int handlerSum; // 总共多少个消费者

    public LongEventHandler(int ordinal, int handlerSum) {
        this.ordinal = ordinal;
        this.handlerSum = handlerSum;
    }

    @Override
    public void onEvent(LongEvent longEvent, long sequence, boolean endOfBatch) throws Exception {
        if ((sequence % handlerSum) == ordinal) {
            //这是我的菜，开始业务处理
            boolean chk = true;
            long l = longEvent.getValue();
            System.out.println("第" + ordinal + "开始处理数据：" + longEvent.getValue() + "");

            Date dtStart = new Date();
            while (chk) {
                Date dt = new Date();
                if (dt.getTime() - dtStart.getTime() >= 100) {
                    chk = false;
                    System.out.println("第" + ordinal + "结束处理数据：" + longEvent.getValue() + "");
                }
            }
        }
    }

}
